<template>
  <div style="width: 100%;height: 100vh;background-color: deepskyblue;overflow: hidden">
    <div style="width: 400px;margin: 150px auto;">
      <div style="color: aliceblue;font-size: 30px;text-align: center;padding: 30px 0">欢迎登录</div>

      <el-form ref="form" :model="form" size="large" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username">
            <template #prefix>
              <el-icon size="150" style="top:15px">
                <UserFilled/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" show-password>
            <template #prefix>
              <el-icon size="50" style="top:15px">
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;font-size: 25px" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {User, UserFilled, Search, Lock} from '@element-plus/icons-vue'
import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      }
    }
  },
  components: {
    User, UserFilled, Search, Lock
  },
  methods: {
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/login", this.form).then(res => {
            if (res.resultCode === 0) {
              this.$message({
                type: "success",
                message: "登录成功"
              })
              console.log(res);
              sessionStorage.setItem("user",JSON.stringify(res.data)) //  缓存用户信息
              this.$router.push("/") //登录成功页面跳转
            } else {
              this.$message({
                type: "error",
                message: "失败"
              })
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>

</style>
